import Vue from 'vue'
import Router from 'vue-router'
import store from '@/vuex/store'
import * as types from '@/vuex/types'
import {mapState} from 'vuex'

Vue.use(Router);

 const routes=[
  {//进入APP初始页
      path:'/',
      redirect:to=>{
        const direct=localStorage.direct;
        if(direct){
            return {name:'login'}
          }else{
            return {path:'/guid'}
        }
    }
   },
   {//引导页
    path:'/guid',
    name:'guid',
    component:() => import('@/view/guid'),
   },
   {//登录注册
    path:'/home',
    // name:'home',
    component:() => import('@/view/loginAndRegister'),
    children:[
      {//登录
        path:'',
        name:'login',
        component:()=>import('@/view/loginAndRegister/login')
      },
       {//短信登录
        path:'msgLogin',
        name:'msgLogin',
        component:()=>import('@/view/loginAndRegister/msgLogin')
      },
      {//注册
        path:'register',
        name:'register',
        component:()=>import('@/view/loginAndRegister/register')
      },
       {//修改密码
        path:'change',
        name:'change',
        component:()=>import('@/view/loginAndRegister/changePwd')
      },
    ]
   },
    {   //首页
      path:'/chat',
      // meta: {requireAuth: true},
      component:()=> import('@/view/chat'),
      children:[
        {
          path:'',
          name:'home',
          // meta:{requireAuth:true},
          component:()=>import('@/view/chat/home')
        },
        {
          path:'advertisingPush',//广告推送卡片
          name:'advertisingPush',
          meta:{requireAuth:true},
          component:()=>import("@/view/chat/advertisingPush")
        },
        {
          path:'msg',
          name:'msgList',
          meta:{requireAuth:true},
          component:()=>import('@/view/chat/advertisingPush/msgList') //聊天信息界面
        },
      ]
    },
    {//有约
      path:'/appointment',
      component:()=>import('@/view/appointment'),
      children:[
        {
          path:'',
          name:'appointmentMain',
          component:()=>import('@/view/appointment/detail')//有约列表
        },
        {
          path:"new",
          name:"newAppointment",
          component:()=>import('@/view/appointment/newAppointment')//新建有约
        },
        {
          path:"apply",
          name:"appointmentApply",
          component:()=>import("@/view/appointment/apply")//申请列表
        },
        {
          path:"map",
          name:"appointMap",
          meta:{requireAuth:true},
          component:()=>import("@/view/appointment/map")//有约地图
        },
        {
          path:"list",
          name:"appointMember",
          meta:{requireAuth:true},
          component:()=>import("@/view/appointment/list")//群成员
        },
        {
          path:"inviteList",
          name:"appointInviteList",
          meta:{requireAuth:true},
          component:()=>import("@/view/appointment/inviteList")//邀请列表
        }
      ]
    },
    {//关注
      path:'/focus',
      component:()=>import('@/view/focus'),
      children:[
        {
          path:'',
          name:'focus',
          // meta:{requireAuth:true},
          component:()=>import('@/view/focus/detail')//关注列表
        },
        {
          path:'apply',
          name:"focusApply",
          meta:{requireAuth:true},
          component:()=>import("@/view/focus/apply")//分组管理
        },
        {
          path:"groupMap",
          name:'focusGroupMap',
          meta:{requireAuth:true},
          component:()=>import("@/view/focus/groupmap")//分组地图
        },
        {
          path:'usermap',
          name:"userMap",
          meta:{requireAuth:true},
          component:()=>import("@/view/focus/userMap")//用户个人地图
        },
        {
          path:"myFocused",
          name:'myfocused',
          meta:{requireAuth:true},
          component:()=>import("@/view/focus/myfocused")//关注我的
        }
      ]
    },
    {//好友
      path:"/friend",
      component:()=>import('@/view/friend'),
      children:[
        {
          path:'',
          name:'friend',
          meta:{requireAuth:true},
          component:()=>import("@/view/friend/group")//好友列表
        },
        {
          path:"apply",
          name:'applyList',
          meta:{requireAuth:true},
          component:()=>import('@/view/friend/apply')//申请列表
        },
        {
          path:'map',
          name:'friendMap',
          meta:{requireAuth:true},
          component:()=>import('@/view/friend/map')//地图
        }
      ]
    },
    {
      path:'/greet',//打招呼板块
      component:()=>import('@/view/greet'),
      children:[
        {
          path:'',
          name:'around',
          meta:{requireAuth:true},
          component:()=>import('@/view/greet/around') //周边
        },
        {
          path:'list',
          name:'uList',
          meta:{requireAuth:true},
          component:()=>import('@/view/greet/userList') //公用用户列表
        },
        {
          path:'msg',
          name:'latestMsg',
          meta:{requireAuth:true},
          component:()=>import('@/view/greet/msg') //聊天信息界面
        },
        {
          path:'family',
          name:'family',
          meta:{requireAuth:true},
          component:()=>import('@/view/greet/family') //家人
        }
      ]
    },
    {   //资讯
      path:'/information',
      // name:'information',
      meta: {requireAuth: true},
      component:()=> import('@/view/information'),
      children:[
        {
          path:'',
          name:'nearby',
          component:()=>import('@/view/information/content')
        },
        {   //往评论回复跳转
          path:'read_reply',
          name:'read_reply',
          component:()=>import('@/components/comment/reply')
        },
        {   //往新闻详情跳转
          path:'newsContent',
          name:'newsContent',
          component:()=>import('@/view/information/content/dynamicDetails/newsContent.vue')
        },
        {   //往普通动态详情跳转
          path:'textContent',
          name:'textContent',
          component:()=>import('@/view/information/content/dynamicDetails/textContent.vue')
        },
        {   //往视频详情跳转
          path:'videoContent',
          name:'videoContent',
          component:()=>import('@/view/information/content/dynamicDetails/videoContent.vue')
        }
      ]
    },
    {   //找一找
      path:'/query',
      // name:'query',
      meta: {requireAuth: true},
      component:()=> import('@/view/query'),
      children:[
        {
          path: '',  //找一找首页
          meta: {requireAuth: true},
          component: () => import('@/view/query/list')
        },
        {
          path:'issue',  //发布投送卡
          name:'issue',
          meta: {requireAuth: true},
          component: () => import('@/view/query/issue')
        },
        {
          path:'contentList',  //查询附近投送卡
          name:'contentList',
          meta: {requireAuth: true},
          component: () => import('@/view/query/list/contentList')
        },
        {
          path:'queryIssueList',  //搜索投送卡
          name:'queryIssueList',
          meta: {requireAuth: true},
          component: () => import('@/view/query/queryList')
        }
      ]
    },
    {  //设置
      path:'/setUp',
      // name:'setUp',
      meta: {requireAuth: true},
      component:()=> import('@/view/setUp'),
      children:[
        {
          path:'',
          name:'mySelf',
          meta:{requireAuth:true},
          component:()=>import('@/view/setUp/mySelf')
        },
        {
          path:'/qr',
          name:'qr',
          meta:{requireAuth:true},
          component:()=>import('@/view/setUp/qrcode')
        }
      ]
    },
   {   //发布路由
     path:'/release',
     // name:'release',
     meta: {requireAuth: true},
     component:()=> import('@/view/release'),
     children:[
       {   //发布视频
         path:'videoRelease',
         name:'videoRelease',
         meta:{requireAuth:true},
         component:()=>import('@/view/release/videoRelease')
       },
       {   //发布动态
         path:'dynamicRelease',
         name:'dynamicRelease',
         meta:{requireAuth:true},
         component:()=>import('@/view/release/dynamicRelease')
       }
     ]
   },
   {
     path:'/common',
     meta:{requireAuth:true},
     component:()=>import('@/view/common'),//公共路由
     children:[
       {
         path:"userIntr",//用户简介
         name:'userIntr',
         meta:{requireAuth:true},
         component:()=>import('@/view/common/userIntr')
       },
       {
         path:'newgroup',//新建分组
         name:'newgroup',
         meta:{requireAuth:true},
         component:()=>import('@/view/common/newGroup')
       },
       {
         path:'list',//列表
         name:'commonList',
         meta:{requireAuth:true},
         component:()=>import('@/view/common/list')
       },
       {
         path:'uset',//用户资料设置
         name:"userSet",
         meta:{requireAuth:true},
         component:()=>import('@/view/common/userSet')
       },
       {
         path:'invita',//邀请列表
         name:'invitaList',
         meta:{requireAuth:true},
         component:()=>import('@/view/common/invitaList')
       },
       {
        path:'add',
        meta:{requireAuth:true},
        name:'searchUser',
        component:()=>import('@/view/common/search'),//搜索用户
      }
     ]
   }
  ];
const router = new Router({
  routes
});
// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
  store.commit(types.LOGIN, window.localStorage.getItem('token'))
}
router.beforeEach((to, from, next) => {
  // next();
  if(to.matched.some(r=>r.meta.requireAuth)){
    // next();
    if (store.state.token) {
    /* 显示加载中动画 */
      store.commit('updateLoadingStatus', true);
      next();
  }
  else {
      next({
          path: '/home',
          query: {redirect: to.fullPath}
      })
      console.log(store.state.token)
    }
  }else{
    next();
  }

});

router.afterEach(route => {
  /* 隐藏加载中动画 */
  store.commit('updateLoadingStatus', false);
});

export default router;
